var markerArray = new Array();
var labelArray = new Array();
var markerClusterer;
var mapIconShow = {};
function MapHandler(_map, mapPath) {
    this.map = _map;
    this.gpsDataMap = {}; //保存当前的实时数据,以GpsRealData表的Id为主键

    this.markerMap = {}; //保存当前的实时marker,以GpsRealData表的Id为主键
    this.areaMap = {}; //保存当前的区域overlay和文本标注,已Enclosure围栏表的id为主键.


    this.mapPath = mapPath;
    this._endIconImageUrl = this.mapPath + "/images/track/end.gif"; //结束地点图标

    this.realDataMap = {};

    this.realDataInfoWindowMap = {};

    //轨迹中回放中的marker, 线等，便于批量清楚
    this.routeOverlays = new Array();
}
/**
 * 将区域对象和对应的文本标注，保存在hash表中，便于调用和删除
 * @enclosureId, 围栏的主键Id
 */
MapHandler.prototype.cacheAreaOverlay = function (enclosureId, overlay, label) {
    this.areaMap[enclosureId] = {overlay: overlay, label: label};
}
MapHandler.prototype.getAreaOverlay = function (enclosureId) {
    return this.areaMap[enclosureId];
}

/**
 * 根据车速、方向、报警和上线状态来生成车辆地标
 */
MapHandler.prototype.getRecordImageUrl = function (direction, velocity, online, alarm) {
    if (!direction) {
        direction = 0;
    }

    var icon11 = {};

    if (online == 4) {
        var img = setAngleImage1(direction);
        icon11 = L.icon({
            iconUrl:this.mapPath + "/images/alarm/" + img
        })
        return icon11;
    }
    if (online == 3) {

        /*	if (typeof (alarm) != "undefined" && parseInt(alarm) != 0) {
         return this.mapPath + "/images/alarm/" + img;
         }
         else*/
        if (velocity <= 1) {
            icon11 = L.icon({
                iconUrl:this.mapPath + "/images/direction/parking/" + "parking.png"
            })

            return icon11;
        }
        else {
            var img = setAngleImage1(direction);
            icon11 = L.icon({
                iconUrl:this.mapPath + "/images/direction/" + img
            })
            return icon11;
        }
    }

    else {
        icon11 = L.icon({
            iconUrl:this.mapPath + "/images/direction/off/" + "offline.png"
        })
        return icon11;
    }

}


MapHandler.prototype.getHisRecordImageUrl = function (direction, velocity, online, alarm) {
    if (!direction) {
        direction = 0;
    }

    if (online == 3) {

        if (velocity <= 1) {
            return this.mapPath + "/images/direction/parking/" + "parking.png";
        }
        else {
            return this.mapPath + "/images/direction/" + "0.png";
        }
    }

    else {
        return this.mapPath + "/images/direction/off/" + "offline.png";
    }

}


MapHandler.prototype.getIconUrlByState = function (state) {
    var iconurl = new String();
    switch (state) {
        case 5:
            iconurl = this.mapPath + "/MapIcon/3-32.png";
            break;
        case 6:
            iconurl = this.mapPath + "/MapIcon/1-32.png";
            break;
        case 7:
            iconurl = this.mapPath + "/MapIcon/4-32.png";
            break;
        case 8:
            iconurl = this.mapPath + "/MapIcon/2-32.png";
            break;
        case 9:
            iconurl = this.mapPath + "/MapIcon/5-32.png";
            break;
        case 10:
            iconurl = this.mapPath + "/MapIcon/6-32.png";
            break;
        case 11:
            iconurl = this.mapPath + "/MapIcon/7-32.png";
            break;
        case 12:
            iconurl = this.mapPath + "/MapIcon/9-32.png";
            break;
        case 13:
            iconurl = this.mapPath + "/MapIcon/10-32.png";
            break;
        default:
            iconurl = this.mapPath + "/MapIcon/8-32.png";
    }
    return iconurl;

}


MapHandler.prototype.getAlarmIconUrlByAlarmType = function (AlarmTypeForIcon) {
    var iconurl = new String();
    switch (AlarmTypeForIcon) {
        case 'BDSOS':
            iconurl = this.mapPath + "/MapIcon/10-32.png";
            break;
        default:
            iconurl = this.mapPath + "/MapIcon/4-32.png";
    }
    return iconurl;

}


//**************************************************************带数据表格的轨迹绘制
MapHandler.prototype.drawRoute = function (record) {
    record = convertRecord(record);
    var courseLat = parseFloat(record.latitude);
    var courseLng = parseFloat(record.longitude);
    if (this.startMarker == null) {
        var iconPath = this.mapPath + "/images/track/start.gif";
        //var icon = this.createIcon(iconPath, 39, 25);
        this.startMarker = this.addHisGpsMarker(record.longitude, record.latitude,
            iconPath, record, false);
        this.routeOverlays.push(this.startMarker);

        this._preLat = record.latitude;
        this._preLng = record.longitude;
        this._preRecord = record;
    } else {
        // 这一次循环的时间
        var time = record.sendTime;

        var imageUrl = this.getHisRecordImageUrl(record.direction, record.velocity, 3, record.alarm);

        var currentPos = new google.maps.LatLng(record.latitude, record.longitude);

        if (this.courseMarker == null) {
            //var icon = this.createIcon(imageUrl, 32);
            this.courseMarker = this.addHisGpsMarker1(record.longitude, record.latitude,
                imageUrl, record, false);

            this.routeOverlays.push(this.courseMarker);
            this.setCenter(record.latitude, record.longitude);
            if (record.velocity > 0) {
                //var icon1 = this.createIcon(this.mapPath + "/images/direction/" + "0.png", 32);
                var iconPath = this.getRecordImageUrl(record.direction, record.velocity,
                    3, record.alarm);
                this.courseMarker.setIcon(iconPath);
                //this.courseMarker.setRotation(record.direction);
            }

        } else {
            this.updateMarker(this.courseMarker, record);
            this.courseMarker.setPosition(currentPos);

            if (record.velocity > 0) {
                var iconPath = this.getRecordImageUrl(record.direction, record.velocity,
                    3, record.alarm);
                this.courseMarker.setIcon(iconPath);
            }
        }
        /*	if (this.hisCarLabel) {
         this.map.removeOverlay(this.hisCarLabel);
         }*/
        var descr = record.simNo + "时间:" + record.sendTime;
        //this.hisCarLabel = this.createMarkerLabel(record.simNo, record.latitude, record.longitude, descr, 32);
        if (this.isInMapBound(currentPos) == false) {
            this.setCenter(record.latitude, record.longitude);
        }
        // 绘制前一点的图标
        var currentMarker;
        var prePos = new google.maps.LatLng(this._preLat, this._preLng);
        var distance = this.distance(this._preLat, this._preLng, record.latitude, record.longitude);
        if (distance > 20) {
            var tempImageUrl = this.mapPath + "/images/toolbar/point.png";
            //var icon = this.createIcon(tempImageUrl);
            var dvsDirectionMarker = this.addHisGpsMarker(this._preLng, this._preLat,
                tempImageUrl, this._preRecord, false);
            //压入到轨迹层，便于一次性清除轨迹
            this.routeOverlays.push(dvsDirectionMarker);
            if (distance < 100000) {
                //画线
                var line = this.createPolyline([prePos,
                    currentPos]);
            }
            this._preLat = record.latitude;
            this._preLng = record.longitude;
            this._preRecord = record;
            //压入到轨迹层，便于一次性清除轨迹
            this.routeOverlays.push(line);
        }

        //if (this._nCount > 1) {
        //}


    }
}

MapHandler.prototype.distance = function (sX, sY, eX, eY) {
    var lat = [sX, eX]
    var lng = [sY, sY] //var R = 6371; // km (change this constant to get miles)
    var R = 6378137; // In meters
    var dLat = (lat[1] - lat[0]) * Math.PI / 180;
    var dLng = (lng[1] - lng[0]) * Math.PI / 180;
    var dLat1 = lat[0] * Math.PI / 180;
    var dLat2 = lat[1] * Math.PI / 180;
    var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(dLat1) * Math.cos(dLat1) * Math.sin(dLng / 2) * Math.sin(dLng / 2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    var d = R * c;
    return Math.round(d);
}


MapHandler.prototype.drawRouteMonitoringAlone = function (record) {
    record = convertRecord(record);
    var courseLat = parseFloat(record.latitude);
    var courseLng = parseFloat(record.longitude);

    if (this.startMarker == null) {
        var iconPath = this.mapPath + "/images/track/start.gif";
        var icon = this.createIcon(iconPath, 39, 25);
        this.startMarker = this.addMarker(record.longitude, record.latitude,
            icon, record, false);
        this.routeOverlays.push(this.startMarker);

        this._preLat = record.latitude;
        this._preLng = record.longitude;
        this._preRecord = record;
    } else {
        // 这一次循环的时间
        var time = record.sendTime;

        var imageUrl = this.getRecordImageUrl(record.direction, record.velocity, true, record.alarm);

        var currentPos = new BMap.Point(record.longitude, record.latitude);

        if (this.courseMarker == null) {
            var icon = this.createIcon(imageUrl, 32);
            this.courseMarker = this.addMarker(record.longitude, record.latitude,
                icon, record, false);

            this.routeOverlays.push(this.courseMarker);
            this.setCenter(record.latitude, record.longitude);
            this.courseMarker.setRotation(record.direction);
        } else {
            this.courseMarker.setPosition(currentPos);
            this.courseMarker.setRotation(record.direction);

        }
        if (this.hisCarLabel) {
            this.map.removeOverlay(this.hisCarLabel);
        }
        var descr = record.simNo + ",车速:" + record.velocity + "km/h,时间:" + record.sendTime;
        this.hisCarLabel = this.createMarkerLabel(record.simNo, record.latitude, record.longitude, descr, 32);
        if (this.isInMapBound(currentPos) == false) {
            this.setCenter(record.latitude, record.longitude);
        }

        // 绘制前一点的图标
        var currentMarker;

        var prePos = new BMap.Point(this._preLng, this._preLat);
        var distance = this.map.getDistance(prePos, currentPos);
        if (distance > 20) {

            var tempImageUrl = this.mapPath + "/images/toolbar/point.png";
            var icon = this.createIcon(tempImageUrl);
            var dvsDirectionMarker = this.addMarker(record.longitude, record.latitude,
                icon, record, false);
            //压入到轨迹层，便于一次性清除轨迹
            this.routeOverlays.push(dvsDirectionMarker);
            //画线
            var line = this.createPolyline([prePos,
                currentPos]);
            this._preLat = record.latitude;
            this._preLng = record.longitude;
            this._preRecord = record;
            //压入到轨迹层，便于一次性清除轨迹
            this.routeOverlays.push(line);
        }


    }
}


/**
 * 停止轨迹播放
 */
MapHandler.prototype.drawStop = function () {
    if (this.courseMarker == null) {
        return;
    }
    /*	var icon = this.createIcon(this._endIconImageUrl, 39, 25);
     this.endMarker = this.addHisGpsMarker(this.courseMarker.getPosition().lng, this.courseMarker.getPosition().lat, icon);
     this.routeOverlays.push(this.endMarker);*/
    //this.map.removeOverlay(this.courseMarker);
    this.startMarker = null;
    this.courseMarker = null;
}

/**
 * 清楚所有的元素
 */
MapHandler.prototype.clearAllElement = function () {
    if (this.hisCarLabel) {
        this.map.removeOverlay(this.hisCarLabel);
    }
    this.startMarker = null;
    this.courseMarker = null;

    //清楚轨迹中的地标
    for (var m = 0; m < this.routeOverlays.length; m++) {
        var overlay = this.routeOverlays[m];
        //this.map.removeOverlay(overlay); //清除地图marker
        overlay.setMap(null);
    }
    routeOverlays = []; //清空数组
}


/**
 * 清楚所有的车辆元素
 */
MapHandler.prototype.clearAllVehicle = function () {
    for (var m in this.realDataMap) {
        var rd = this.realDataMap[m];
        //his.map.removeOverlay(rd.marker);
        rd.marker.setMap(null);
        //this.map.removeOverlay(rd.label);
    }
    this.realDataMap = {};

}

/**
 * 根据id删除电子围栏
 */
MapHandler.prototype.removeMapArea = function (enclosureId) {
    var area = this.areaMap[enclosureId];
    if (area) {
        //this.map.removeOverlay(area.overlay);
        area.overlay.setMap(null);
        //this.map.removeOverlay(area.label);
        area.label.close();
    } else {
        //alert("该区域不存在，无法删除");
    }
}

MapHandler.prototype.setCenter = function (lat, lng, zoom) {
    if (!zoom)
        zoom = this.map.getZoom();
    var center = L.latLng(lat, lng)
    // this.map.setCenter(center, 19);
    this.map.panTo(center, {});
    this.map.setZoom(19,{})
}


/**
 * 批量在地图上显示gps车辆地标
 * @gpsDataArray页面传送的gps数据，可以同时显示多个地标，所以是数组
 * @isOpenInfoWindow 是否打开信息窗口
 */
MapHandler.prototype.showMarkers = function (gpsDataArray, isOpenInfoWindow) {
    //for (var i = 0; i < gpsDataArray.length; i++) {
    var gpsData = gpsDataArray[0];
    if (gpsData.longitude > 1 && gpsData.latitude > 1) {
        //continue;

        //var currentPos = new BMap.Point(gpsData.dvsLongitude, gpsData.dvsLatitude);
        //在地图边界外的不进行刷新
        //if(this.isInMapBound(currentPos) == false)
        // continue;
        //var oldRealDataMarker = this.realDataMap[gpsData.dvcSim];
        //var descr = gpsData.dvcSim + "[" + gpsData.depName + "]";
        /**if (gpsData.dvsOnline) {
		if (gpsData.dvsSpeed == 0)
		descr += "停车中";
		else
		descr += "车速:" + gpsData.dvsSpeed + "km/h";
		} else
         descr += "离线中";**/


        var iconPath = this.getRecordImageUrl(gpsData.direction, gpsData.velocity, gpsData.online, gpsData.alarmState);
        var icon = this.createIcon(iconPath, 32);
        // isOpenInfoWindow = false;
        var marker = this.addRealDataMarker(gpsData.longitude, gpsData.latitude, icon, gpsData, isOpenInfoWindow);

        //labelArray[gpsData.dvcSim] = label;
        //if(isCenter)
        //this.setCenter(gpsData.dvsLatitude, gpsData.dvsLongitude);

    }
    //this.addGoogleMarker(marker);
    //}
}

MapHandler.prototype.showMarkers_1 = function (gpsDataArray, isOpenInfoWindow) {
    var markers = [];
    for (var i = 0; i < gpsDataArray.length; i++) {
        var gpsData = gpsDataArray[i];
        if (gpsData.latitude > 0 && gpsData.longitude > 0) {
            var currentPos = L.LatLng(gpsData.latitude, gpsData.longitude)
            // var currentPos = new google.maps.LatLng(gpsData.latitude, gpsData.longitude);
            var oldRealDataMarker = this.realDataMap[gpsData.simNo];
            var marker = null;
            if(gpsData.online == 0){
                gpsData.velocity = 0;
            }
            if(gpsData.velocity > 120){
                gpsData.velocity = 100;
            }
            var descr = gpsData.equipName + ",速度:" + gpsData.velocity + "km/h";
            //var label = this.createMarkerLabel1(gpsData.simNo, gpsData.longitude, gpsData.latitude, descr, 32);
            if (oldRealDataMarker != null) {

                //this.map.removeOverlay(oldRealDataMarker.marker);
                //this.map.removeOverlay(oldRealDataMarker.label);
                oldRealDataMarker.marker.setPosition(currentPos);
                var iconPath = this.getRecordImageUrl(gpsData.direction, gpsData.velocity,
                    gpsData.online, gpsData.alarmState);
                //	var icon = this.createIcon(iconPath, 32);
                oldRealDataMarker.marker.setIcon(iconPath);
                //oldRealDataMarker.marker.getIcon().setImageUrl(iconPath);
                //oldRealDataMarker.label.setPosition(currentPos);

                /*	if (gpsData.velocity > 1) {
                 //console.info(oldRealDataMarker.marker);
                 oldRealDataMarker.marker.setRotation(gpsData.direction);
                 } else {
                 oldRealDataMarker.marker.setRotation(0);
                 }*/
                oldRealDataMarker.marker.label.setContent1(descr);
                oldRealDataMarker.gpsData = gpsData;
                var htmlContent = window.parent.renderTemplate(gpsData);
                this.realDataInfoWindowMap[gpsData.simNo].markerInfoWindow.setContent(htmlContent);
                this.realDataInfoWindowMap[gpsData.simNo].markerInfoWindow.close(this.map, oldRealDataMarker.marker);
                /*	this.realDataInfoWindowMap[gpsData.simNo].markerInfoWindow.close(this.map, oldRealDataMarker.marker);
                    var htmlContent = window.parent.renderTemplate(gpsData);
                    console.info(htmlContent);
                    var markerInfoWindow = new google.maps.InfoWindow({content: htmlContent});
                    google.maps.event.addListener(oldRealDataMarker.marker, "click", function () {
                        console.info(this.realDataInfoWindowMap);
                        this.realDataInfoWindowMap[gpsData.simNo].markerInfoWindow.close(this.map, oldRealDataMarker.marker);
                        markerInfoWindow.open(this.map, oldRealDataMarker.marker); //把这个infoWindow绑定在选定的marker上面
                    });*/
                //oldRealDataMarker.marker.markerInfoWindow.close();
                //markers.push(oldRealDataMarker.marker);
            } else {
                if (markerClusterer != null) {
                    markerClusterer.clearMarkers();
                }
                var iconPath = this.getRecordImageUrl(gpsData.direction, gpsData.velocity,
                    gpsData.online, gpsData.alarmState);
                var marker = this.addRealDataMarker_1(gpsData.longitude, gpsData.latitude, iconPath, gpsData, isOpenInfoWindow);
                this.realDataMap[gpsData.simNo] = {marker: marker, gpsData: gpsData};
                mapIconShow[gpsData.simNo] = {marker: marker, gpsData: gpsData};
                //markerArray[gpsData.dvcSim] = marker;
                markers.push(marker);
                //if(isCenter)
                //this.setCenter(gpsData.latitude, gpsData.longitude);
            }
        }
        //this.addGoogleMarker(marker);
    }

    if (markers.length > 0) {
        markerClusterer = new MarkerClusterer(this.map, markers, {imagePath: 'map/google/images/m'});
    }
}


/**
 * 点击分组清除地图车辆图标
 */
MapHandler.prototype.clearMarkers = function (gpsDataArray, isOpenInfoWindow) {
    var markers = [];
    for (var i = 0; i < gpsDataArray.length; i++) {
        var gpsData = gpsDataArray[i];
        var rd = mapIconShow[gpsData.simNo];
        if (typeof (rd) == "undefined")
            continue;
        markers.push(rd.marker);
        //this.map.removeOverlay(rd.marker);
        delete this.realDataMap[gpsData.simNo];
    }

    markerClusterer.removeMarkers(markers);
}


/**
 * 点击设备清除地图车辆图标
 */
MapHandler.prototype.clearMarkerDevice = function (sim) {
    var rd = mapIconShow[sim];
    markerClusterer.removeMarker(rd.marker);
    delete this.realDataMap[sim];
}


/**
 * 点击设备清除地图车辆图标
 */
MapHandler.prototype.clearMarkerDeviceSearch = function (sims) {
    var str = new Array();
    str = sims.split(",");
    var markers = [];
    for (i = 0; i < str.length; i++) {
        var sim = str[i];
        var rd = mapIconShow[sim];
        if (typeof (rd) == "undefined")
            continue;
        markers.push(rd.marker);
        delete this.realDataMap[sim];
    }
    markerClusterer.removeMarkers(markers);

}

/**
 * 批量在地图上显示gps车辆地标
 * @gpsDataArray页面传送的gps数据，可以同时显示多个地标，所以是数组
 * @isOpenInfoWindow 是否打开信息窗口
 */
MapHandler.prototype.centerMarkerOverlays = function (gpsDataArray, isOpenInfoWindow, isCenter) {
    for (var i = 0; i < gpsDataArray.length; i++) {
        var gpsData = gpsDataArray[i];
        if (gpsData.longitude < 1 || gpsData.latitude < 1 || gpsData.latitude == null)
            continue;
        var oldRealDataMarker = this.realDataMap[gpsData.simNo];
        //var descr = gpsData.dvsSim + "[" + gpsData.depName + "]";
        /**if (gpsData.dvsOnline) {
		if (gpsData.dvsSpeed == 0)
		descr += "停车中";
		else
		descr += "车速:" + gpsData.dvsSpeed + "km/h";
		} else
         descr += "离线中";**/
        if (oldRealDataMarker != null) {
            var currentPos = new google.maps.LatLng(gpsData.latitude, gpsData.longitude);
            oldRealDataMarker.marker.setPosition(currentPos);
            var iconPath = this.getRecordImageUrl(gpsData.direction, gpsData.velocity, gpsData.online, gpsData.alarmState);
            //oldRealDataMarker.marker.getIcon().setImageUrl(iconPath);
            //var icon = this.createIcon(iconPath, 32);
            oldRealDataMarker.marker.setIcon(iconPath);
            //oldRealDataMarker.label.setPosition(currentPos);
            /*	if (gpsData.velocity > 1) {
             oldRealDataMarker.marker.setRotation(gpsData.direction);
             } else {
             oldRealDataMarker.marker.setRotation(0);
             }*/
            //oldRealDataMarker.label.setContent(descr);
            oldRealDataMarker.gpsData = gpsData;
            var htmlContent = window.parent.renderTemplate(gpsData);
            var markerInfoWindow = new google.maps.InfoWindow({content: htmlContent});
            markerInfoWindow.open(this.map, oldRealDataMarker.marker); //把这个infoWindow绑定在选定的marker上面
        } else if (!isNaN(gpsData.latitude)) {

            var iconPath = this.getRecordImageUrl(gpsData.direction, gpsData.velocity, gpsData.online, gpsData.alarmState);
            //var icon = this.createIcon(iconPath, 32);
            // isOpenInfoWindow = false;
            //alert(gpsData.dvsLongitude);
            var marker = this.addRealDataMarker(gpsData.longitude, gpsData.latitude, iconPath, gpsData, isOpenInfoWindow);
            /*	if (gpsData.velocity > 1) {
             marker.setRotation(gpsData.direction);
             } else {
             marker.setRotation(0);
             }*/
            //var label = this.createMarkerLabel(gpsData.dvcSim, gpsData.dvsLatitude, gpsData.dvsLongitude, descr, 32);
            this.realDataMap[gpsData.simNo] = {marker: marker, gpsData: gpsData};
        }
        if (!isNaN(gpsData.latitude))
            if (isCenter)
                this.setCenter(gpsData.latitude, gpsData.longitude);
        //this.addGoogleMarker(marker);
    }
}


/**
 * 点击报警数据，展示在地图
 */
MapHandler.prototype.centerAlarmMarkerOverlays = function (alarm, isOpenInfoWindow, isCenter) {
    var oldRealDataMarker = this.realDataMap[alarm.simNo];
    if (oldRealDataMarker != null) {
        var currentPos = new BMap.Point(alarm.longitude, alarm.latitude);
        oldRealDataMarker.marker.setPosition(currentPos);
        var iconPath = this.getAlarmIconUrlByAlarmType(alarm.AlarmTypeForIcon);
        var icon = this.createIcon(iconPath, 32);
        oldRealDataMarker.marker.setIcon(icon);
        oldRealDataMarker.gpsData = alarm;
        var htmlContent = window.parent.renderTemplateAlarm(alarm);
        var markerInfoWindow = new BMap.InfoWindow(htmlContent);
        oldRealDataMarker.marker.openInfoWindow(markerInfoWindow);
    } else {

        var iconPath = this.getAlarmIconUrlByAlarmType(alarm.AlarmTypeForIcon);
        var icon = this.createIcon(iconPath, 32);
        var marker = this.addRealDataAlarmMarker(alarm.longitude, alarm.latitude, icon, alarm, isOpenInfoWindow);
        this.realDataMap[alarm.simNo] = {marker: marker, gpsData: alarm};

    }
    if (isCenter)
        this.setCenter(alarm.latitude, alarm.longitude);

}


MapHandler.prototype.planDriveRoute = function (startPOI, endPOI) {
    var driving = new BMap.DrivingRoute(this.map, {
        renderOptions: {
            map: this.map,
            panel: "r-result",
            autoViewport: true
        }
    });
    driving.search(startPOI, endPOI);

}

MapHandler.prototype.createLocalSearch = function (onSearchResult) {
    var me = this;
    var options = {
        onSearchComplete: function (results) {
            // 判断状态是否正确
            if (search.getStatus() == BMAP_STATUS_SUCCESS) {
                var poiList = [];
                for (var i = 0; i < results.getCurrentNumPois(); i++) {
                    var poi = results.getPoi(i);
                    var name = poi.title + ", " + results.getPoi(i).address;
                    var code = i;
                    poiList.push({result: name, value: poi.address, data: poi});
                }
                onSearchResult(me.keyword, poiList);
            }
        }
    };
    var search = new BMap.LocalSearch('泰州市', options);
    return search;
}

MapHandler.prototype.createVehicleMarker = function (bmPoint, bIcon, opt, isOpenInfoWindow) {
    var marker = new BMap.Marker(bmPoint, {icon: bIcon});  // 创建标注
    map.addOverlay(marker);              // 将标注添加到地图中
    var htmlContent = window.parent.renderTemplate(opt);
    var markerInfoWindow = new BMap.InfoWindow(htmlContent);
    marker.addEventListener("click", function () {
        this.openInfoWindow(markerInfoWindow);
    });
    if (isOpenInfoWindow) {
        marker.openInfoWindow(markerInfoWindow);
    }
    return marker;
}


MapHandler.prototype.createIcon = function (iconPath, width, height) {
    //默认是16*16
    if (!width)
        width = 16;
    if (!height)
        height = width;

    return L.icon({iconUrl:iconPath});
}

MapHandler.prototype.openInfoWindow = function (dvcSim) {
    var realData = this.realDataMap[dvcSim];
    if (realData) {
        var opt = realData.gpsData;
        var htmlContent = window.parent.renderTemplate(opt);
        var markerInfoWindow = new google.maps.InfoWindow({content: htmlContent});
        markerInfoWindow.open(this.map, realData.marker); //把这个infoWindow绑定在选定的marker上面
        this.realDataInfoWindowMap[dvcSim] = {markerInfoWindow: markerInfoWindow};
    }
}

MapHandler.prototype.openInfoWindowAlarm = function (dvcSim) {
    var realData = this.realDataMap[dvcSim];
    if (realData) {
        var opt = realData.gpsData;
        var htmlContent = window.parent.renderTemplateAlarm(opt);
        var markerInfoWindow = new BMap.InfoWindow(htmlContent);
        realData.marker.openInfoWindow(markerInfoWindow);
    }
}

MapHandler.prototype.openHtmlWindow = function (marker, htmlContent) {
    var markerInfoWindow = new BMap.InfoWindow(htmlContent);
    marker.openInfoWindow(markerInfoWindow);
}


MapHandler.prototype.addMarker = function (lng, lat, icon, opt, isOpenInfoWindow) {
    var pt = L.latLng(lng, lat);
    //var myIcon = new BMap.Icon(iconPath, new BMap.Size(16,16));
    var marker = new BMap.Marker(pt, {icon: icon});  // 创建标注
    this.map.addOverlay(marker);              // 将标注添加到地图中
    if (opt) {
        var dvcId = opt.id;
        var me = this;
        var htmlContent = window.parent.renderTemplate(opt);
        marker.addEventListener("click", function () {
            //me.openInfoWindow(dvcId);
            //me.openHtmlWindow(marker,htmlContent);
            var markerInfoWindow = new BMap.InfoWindow({content: htmlContent});
            marker.openInfoWindow(markerInfoWindow);
        });
        if (isOpenInfoWindow) {
            var markerInfoWindow = new BMap.InfoWindow({content: htmlContent});
            marker.openInfoWindow(markerInfoWindow);
        }
    }
    return marker;
}

MapHandler.prototype.addHisGpsMarker = function (lng, lat, icon, opt, isOpenInfoWindow) {
    var pt = L.latLng(lat, lng);
    //var myIcon = new BMap.Icon(iconPath, new BMap.Size(16,16));
    var marker = new MarkerWithLabel({
        position: pt,
        icon: icon
    });
    marker.setMap(this.map);
    if (opt) {
        var dvcId = opt.id;
        var me = this;
        var htmlContent = window.parent.renderTemplateHisGps(opt);
        var markerInfoWindow = new google.maps.InfoWindow({content: htmlContent});
        google.maps.event.addListener(marker, "click", function () {
            markerInfoWindow.open(this.map, marker); //把这个infoWindow绑定在选定的marker上面
        });
        if (isOpenInfoWindow) {
            //	markerInfoWindow.open(this.map, marker); //把这个infoWindow绑定在选定的marker上面
        }
    }
    return marker;
}

MapHandler.prototype.addHisGpsMarker1 = function (lng, lat, icon, opt, isOpenInfoWindow) {
    var pt = L.latLng(lat, lng);
    //var myIcon = new BMap.Icon(iconPath, new BMap.Size(16,16));
    var marker = new MarkerWithLabel({
        position: pt,
        icon: icon
    });
    marker.setMap(this.map);
    return marker;
}

//var infowindow = new google.maps.InfoWindow();
MapHandler.prototype.updateMarker = function (marker, opt) {
    if (opt) {
        var dvcId = opt.id;
        var me = this;
        var htmlContent = window.parent.renderTemplateHisGps(opt);
        // google.maps.event.addListener(marker, 'click', function () {
        //     //this.hisInfoWindowMap[dvcId].infowindow.close(this.map, marker);
        //     infowindow.setContent(htmlContent);
        //     infowindow.open(this.map, marker);
        //
        // });

        marker.on("click",function (e) {
            L.popup().setLatLng(e.latlng).setContent(htmlContent).openOn(this.map)
        })


    }

    return marker;
}


MapHandler.prototype.addRealDataAlarmMarker = function (lng, lat, icon, opt, isOpenInfoWindow) {
    var pt = L.latLng(lng, lat);
    //var myIcon = new BMap.Icon(iconPath, new BMap.Size(16,16));
    var marker = new BMap.Marker(pt, {icon: icon});  // 创建标注
    this.map.addOverlay(marker);              // 将标注添加到地图中
    if (opt) {
        var dvcSim = opt.simNo;
        var me = this;
        marker.addEventListener("click", function () {
            me.openInfoWindowAlarm(dvcSim);
        });
        if (isOpenInfoWindow) {
            var htmlContent = window.parent.renderTemplateAlarm(opt);
            var markerInfoWindow = new BMap.InfoWindow(htmlContent);
            marker.openInfoWindow(markerInfoWindow);
        }
    }
    return marker;
}

MapHandler.prototype.addRealDataMarker = function (lng, lat, icon, opt, isOpenInfoWindow) {

    var pt = L.latLng(lat, lng);
    //var myIcon = new BMap.Icon(iconPath, new BMap.Size(16,16));
    var marker = L.marker(pt,{
        icon:icon,

    }).addTo(this.map)
    //this.map.addOverlay(marker);              // 将标注添加到地图中

    return marker;
}

MapHandler.prototype.addRealDataMarker_1 = function (lng, lat, icon, opt, isOpenInfoWindow) {
    var pt = L.latLng(lat, lng);
    //var myIcon = new BMap.Icon(iconPath, new BMap.Size(16,16));
    // var marker = new MarkerWithLabel({
    //     position: pt,
    //     labelContent: opt.equipName + ",速度:" + opt.velocity + "km/h", //需要显示的标题的内容
    //     //labelAnchor: pt, //当前标题基于marker左上角的偏移
    //     labelStyle: { //标题的style样式
    //         color: "#fff",
    //         fontSize: "12px"
    //     },
    //     labelClass: "labels", //支持直接绑定class名，将样式写在css里面
    //     labelStyle: {opacity: 1},
    //     icon: icon
    // });
    var marker = L.marker(pt,{
        icon:icon,

    }).addTo(this.map)
    //this.map.addOverlay(marker);              // 将标注添加到地图中
    // if (opt) {
    //     var dvcSim = opt.simNo;
    //     var me = this;
    //     var htmlContent = window.parent.renderTemplate(opt);
    //     var markerInfoWindow = new google.maps.InfoWindow({content: htmlContent});
    //     this.realDataInfoWindowMap[dvcSim] = {markerInfoWindow: markerInfoWindow};
    //     google.maps.event.addListener(marker, "click", function () {
    //         markerInfoWindow.open(this.map, marker); //把这个infoWindow绑定在选定的marker上面
    //     });
    //     if (isOpenInfoWindow) {
    //
    //         markerInfoWindow.open(this.map, marker); //把这个infoWindow绑定在选定的marker上面
    //     }
    // }
    return marker;
}


MapHandler.prototype.createMarker = function (lng, lat, iconPath, opt, isOpenInfoWindow) {
    var pt = L.latLng(lng, lat);
    var myIcon = this.createIcon(iconPath);
    var marker = new BMap.Marker(pt, {icon: myIcon});  // 创建标注
    //map.addOverlay(marker);              // 将标注添加到地图中
    var htmlContent = window.parent.renderTemplate(opt);
    var markerInfoWindow = new BMap.InfoWindow(htmlContent);
    marker.addEventListener("click", function () {
        this.openInfoWindow(markerInfoWindow);
    });
    if (isOpenInfoWindow) {
        marker.openInfoWindow(markerInfoWindow);
    }
    return marker;
}
MapHandler.prototype.createPolyline = function (points) {

    var polyline = new google.maps.Polyline({
        path: points,
        strokeColor: "blue", // 线条颜色
        strokeOpacity: 0.5, // 线条透明度
        strokeWeight: 2 // 线条粗细
    });

    polyline.setMap(map);

    return polyline;
}
/**
 * 判断点是否在地图边界中
 */
MapHandler.prototype.isInMapBound = function (bmPoint) {
    var bounds = this.map.getBounds();
    return bounds.contains(bmPoint);
}
/**
 * 创建标注
 */
MapHandler.prototype.createLabel = function (id, lat, lng, text, offsetY) {
    /*	if (!offsetY)
     offsetY = -10;
     var offsetX = -8 * text.length / 2;
     //alert(offsetX);
     var point = new BMap.Point(lng, lat);
     var opts = {
     position: point,    // 指定文本标注所在的地理位置
     offset: new BMap.Size(offsetX, 26)    //设置文本偏移量
     }*/
    var pt = L.latLng(lat, lng);
    var _infoBubble = new InfoBubble({
        shadowStyle: 0,
        padding: 0,
        map: map,
        borderRadius: 0,
        minWidth: 100,
        color: 'blue',
        backgroundColor: 'transparent',
        borderColor: 'transparent',
        disableAutoPan: true,
        hideCloseButton: false,
        setCloseSrc: ''
    });
    _infoBubble.setPosition(pt);
    _infoBubble.setContent(text);
    _infoBubble.open();

    return _infoBubble;
}

MapHandler.prototype.createMarkerLabel = function (id, lat, lng, text, offsetY) {
    if (!offsetY)
        offsetY = -10;
    var offsetX = -8 * text.length / 2;
    //alert(offsetX);
    var point = new google.maps.LatLng(lat, lng);
    var opts = {
        position: point,    // 指定文本标注所在的地理位置
        offset: new google.maps.Size(offsetX, 32)    //设置文本偏移量
    }
    var label = new google.maps.Label(text, opts);  // 创建文本标注对象
    label.setStyle({
        color: "blue",
        fontSize: "12px",
        height: "20px",
        border: 0,
        background: "#CCFFCC",
        lineHeight: "20px",
        fontFamily: "微软雅黑"
    });
    //map.addOverlay(label);
    return label;
}

MapHandler.prototype.createMarkerLabel1 = function (id, lat, lng, text, offsetY) {
    if (!offsetY)
        offsetY = -10;
    var offsetX = -8 * text.length / 2;
    //alert(offsetX);
    var point = new google.maps.LatLng(lat, lng);
    var opts = {
        position: point,    // 指定文本标注所在的地理位置
        offset: new google.maps.Size(offsetX, 32)    //设置文本偏移量
    }
    var label = new google.maps.Label(text, opts);  // 创建文本标注对象
    label.setStyle({
        color: "blue",
        fontSize: "12px",
        height: "20px",
        border: 0,
        background: "#CCFFCC",
        lineHeight: "20px",
        fontFamily: "微软雅黑"
    });
    //map.addOverlay(label);
    return label;
}

/**
 *单独监控的时候，不断的刷新最新的位置图标
 */
MapHandler.prototype.createMarkerOverlayTrackMode = function (targetInfo) {
    //try {
    var currentPos = new BMap.Point(targetInfo.rLng, targetInfo.rLat);
    //过滤无效的日期时间
    //if (parseInt(targetInfo.gpsTime.split(" ")[0].split("-")[0]) < this._defaultYearFilter)
    //return;
    var operatorObj = this;
    // 创建标记
    var vehicledvsDirectionImagUrl = this.mapPath + "/images/dvsDirection/"
        + setAngleImage(targetInfo.angleInt);
    //var currentGpsTimeMill = parseDate(targetInfo.gpsTime).getTime();

    // 绑定标记
    // this._handler.bindClickEvent(targetInfo.marker, targetInfo, this.bindClickEventHTML);
    /**
     this._handler.bindClickEvent(targetInfo.marker, targetInfo, function(opts){
	return that.bindClickEventHTML(opts);
	},function() {
	operatorObj._handler.getAddress(targetInfo.id, targetInfo.rLat, targetInfo.rLng);
	});
     */
    var gpsDataId = targetInfo.plateNo;
    var marker = this.markerMap[gpsDataId];
    var gpsData = this.gpsDataMap[gpsDataId];
    if (!marker) {
        var vehicleIcon = new BMap.Icon(vehicledvsDirectionImagUrl, new BMap.Size(32, 32));
        marker = this.createVehicleMarker(currentPos,
            vehicleIcon, targetInfo, false);

        this.markerMap[gpsDataId] = marker;
        this.setCenter(targetInfo.rLat, targetInfo.rLng, 16);
        this.markerMap[gpsDataId] = marker;
    } else {
        marker.setPosition(currentPos); //更新车辆的当前位置
        var dvsDirectionImagUrl = this.mapPath + "/images/toolbar/point.png";
        var icon = this.createIcon(dvsDirectionImagUrl);
        var dvsDirectionMarker = this.addMarker(gpsData.rLng, gpsData.rLat,
            icon, gpsData, false);
        var prePos = new BMap.Point(gpsData.rLng, gpsData.rLat);
        var distance = this.map.getDistance(prePos, currentPos);
        var points = new Array();
        points[0] = prePos;
        points[1] = currentPos;
        this.createPolyline(points);
        /**
         if(distance < 20)
         {
		 operatorObj._handler
		 .removeOverlay(preTargetInfo.marker); //清楚掉前面的marker,避免密集显示图标
		 //return;
		 }else
         {
		 preTargetInfo.marker.setIconImage(this._currentImageUrl);
		 }*/
    }
    if (this.isInMapBound(currentPos) == false) {
        this.setCenter(targetInfo.rLat, targetInfo.rLng);
    }
    this.gpsDataMap[gpsDataId] = targetInfo;
    //} catch (e) {

    //}
}
